package com.bluestation.misc;

import java.io.*;

import com.bluestation.Environment;

/**
 * Java Shell version 1.0.0
 * 
 * @author seungmun
 * @email choiseungmun@gmail.com
 * @date 2006.1.18.
 * 
 */
public class CsmShell
{
	public CsmShell(InputStream in, PrintStream out, PrintStream err)
	{
		Runtime runtime = Runtime.getRuntime();
		try
		{
			StringBuilder msg = new StringBuilder();
			msg.append("CSM Java Shell version 1.0.0");
			out.println(msg.toString());
			out.println(Environment.GetOsName() + " " + Environment.GetOsVersion() + "[" + Environment.GetOsArch() + "]");
		
			out.println(Environment.GetVmName() + " " + Environment.GetVmVersion());
			out.println("Java " + Environment.GetJavaVersion());
			out.println("Java Home : \"" + Environment.GetJavaHome() + "\"");
			
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String line = null;
			while (true)
			{
				try
				{
					 line = br.readLine();
					if (line == null)
						break;
					
					if (line.equalsIgnoreCase("EXIT"))
					{
						out.println("Exit CSM Java Shell, ...Thanks.");
						break;
					}
					else if( line.equalsIgnoreCase("DIR")){
						File f = new File("./");
						for(File ff : f.listFiles()){
							out.println(ff.getName());
						}
						continue;
					}

					Process p = runtime.exec(line);

					@SuppressWarnings("unused") 
					OutputStream pin = p.getOutputStream();
					InputStream pout = p.getInputStream();
					
					@SuppressWarnings("unused") 
					InputStream perr = p.getErrorStream();

					for (int ch; (ch = pout.read()) != -1;)
						System.out.write(ch);

				}
				catch (IOException e)
				{
					e.printStackTrace(out);
					out.println();
				}
			} // end of while
		}
		catch (Exception ignore)
		{
			ignore.printStackTrace(out);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		new CsmShell(System.in, System.out, System.err);
	}

}
